/**
 *
 */

angular.module('myApp')

    .controller('batchCtrl', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                       CacheService, limitToFilter, filterFilter,myConstant) {
        //视图层变量viewModel
        var vm = $scope.vm = {};
        vm.constant = myConstant;
        //变量初始化
        //分页数据
        vm.pagination = {
            pageSize: 10,
            pageNum: 1,
            list:{}
        };
        //查询条件
        vm.findBean = {};

        //缓存数据初始化(需要缓存的key请自定义)
        /*数据格式{
         key1 :value1,
         key2:value2
         }*/
        vm.cached = {
            CUSTPROPERTY:{},//客户属性
            PERSONAL_RISK_KW: {},//个人关键字
            MERCHANT_RISK_KW: {},//商户关键字
            PERSONAL_RISK_TYPE:{},//个人风险类型
            MERCHANT_RISK_TYPE:{},//商户风险类型
            INFORMATION_LEVEL:{},//信息级别
            VALIDITY:{},//有效性
            RISK_EVENT_CHANNEL:{},//风险事件发生渠道
            //TD_RISK_TYPE:{},//风险类型
            MERCHANT_TYPE:{},//商户类型，
            MERCHANT_PROPERTY:{},// 商户属性，
            LEGAL_CREDENTIALS_TYPE:{},//法人证件类型,
            PO_CREDENTIALS_TYPE:{},// 人员证件类型
            ADDRESS:{}//地域
        };

        //批量获取缓存
        var keys = [];
        for (var key in  vm.cached) {
            keys.push(key);
        }
        $log.debug('批量查询缓存');
        $http.post(config.ctx + '/specially/dataDic/cache/get', keys, config.jsonHeader)
            .then(function (response) {
                var tempMap = response.data.object;
                for (var key in vm.cached) {
                    if (!angular.isUndefined(tempMap[key])) {
                        vm.cached[key] = tempMap[key];
                    }
                }
            }, function (response) {
                $log.error('获取数据%s失败', keys);
            })
        //CacheService.initCache(vm.cached);

        /*将前面缓存的数据格式化
         [{key:value},{key:value}]*/
        vm.getCache = function (key) {
            return CacheService.getCache(key);
        };

        //获取对象数据[user,user,user]
        vm.getObj = function (key, input) {
            return $http.post(config.ctx + '/base/cache/getAll',
                $httpParamSerializerJQLike({cacheKey: key}))
                .then(function (response) {
                    return limitToFilter(filterFilter(response.data.object, input), 6);
                }, function (response) {
                    $log.error('获取数据%s失败', cacheKey);
                })
        };


        vm.resetForm = resetForm;

        /*------------------以上配置通用---------------------*/

        vm.queryDetailed = queryDetailed;
        vm.queryDetail = queryDetail;
        vm.updateChecked = updateChecked;
        /*------------------以上方法名可选择性通用---------------------*/

        function resetForm() {
            vm.findBean = {};
        }

        function updateChecked($event, id) {
            CheckboxService.updateChecked($event, id);
            $log.debug(id);
            $log.debug(CheckboxService.getChecked());
        }

        var modeUrl;
        //查询
        function queryDetail(){
            if(vm.findBean.cusProperty=="02"){
                modeUrl = 'batchList';//02:商户
                vm.cached.TD_RISK_TYPE = vm.cached.MERCHANT_RISK_TYPE;
            }else{
                //01：个人
                modeUrl = 'batchDetailList';
                vm.cached.TD_RISK_TYPE = vm.cached.PERSONAL_RISK_TYPE;
            }
            vm.pagination.list=null;
            var findForm = document.getElementById('findForm');
            vm.url = angular.element(findForm).prop('action');
            if(vm.findBean.cusProperty && vm.findBean.keyWord && vm.findBean.infos){
                //查询数据显示
                var  postData = {};
                postData.cusProperty = vm.findBean.cusProperty;
                postData.keyWord = vm.findBean.keyWord;
                postData.infos = vm.findBean.infos;
                postData.pageIndex = vm.pagination.pageNum;
                postData.pageSize = vm.pagination.pageSize;
                $http.post(vm.url,postData,config.jsonHeader).then(function(response){
                    var data = response.data;
                    if(data.success){
                        vm.pagination.list = data.sumObject
                        vm.pagination.rowCount = 20;
                    }else{
                        alert(data.msg);
                    }
                })
            }else{
                alert("请填写完整查询信息！");
            }
        }
        //选中项改变事件
        $("#q_type").change(function(){
            var v = $("#q_type").val();
            //string:1000001
            v = v.substr(v.indexOf(":")+1);
            if(v =="01"){//个人
                vm.cached.Q_TYPE_CODE = vm.cached.PERSONAL_RISK_KW;
            }else{
                vm.cached.Q_TYPE_CODE =  vm.cached.MERCHANT_RISK_KW;
            }

        });
        function queryDetailed(bean) {
            console.info(vm.cached.ADDRESS);
            var item=bean;
            OpenService({
                modalTitle: '详细信息',
                modalBody: modeUrl,
                url: '#',
                item: item,
                cached: {
                    CUSTPROPERTY:vm.cached.CUSTPROPERTY,
                    INFORMATION_LEVEL:vm.cached.INFORMATION_LEVEL,//信息级别
                    VALIDITY:vm.cached.VALIDITY,//有效性
                    RISK_EVENT_CHANNEL:vm.cached.RISK_EVENT_CHANNEL,//风险事件发生渠道
                    TD_RISK_TYPE:vm.cached.TD_RISK_TYPE,//风险类型
                    MERCHANT_TYPE:vm.cached.MERCHANT_TYPE,//商户类型，
                    MERCHANT_PROPERTY:vm.cached.MERCHANT_PROPERTY,//商户属性
                    LEGAL_CREDENTIALS_TYPE:vm.cached.LEGAL_CREDENTIALS_TYPE,//法人证件类型,
                    PO_CREDENTIALS_TYPE:vm.cached.PO_CREDENTIALS_TYPE,// 人员证件类型
                    ADDRESS:vm.cached.ADDRESS//行政区域
                }
            },function(item){

            },'','myModalNoSave.html');
        }

    })
;



